package com.all.play.myapplication;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.all.play.myapplication.base.BasePager;
import com.all.play.myapplication.page.LocalMusicPager;
import com.all.play.myapplication.page.LocalVideoPager;
import com.all.play.myapplication.page.NetMusicPager;
import com.all.play.myapplication.page.NetVideoPager;
import com.all.play.myapplication.utils.ConMidlesFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private RadioGroup radioGroup;

    /**
     *存储pages的集合
     *@author 聪明一只哈
     *@time 2018/3/28 15:07
     */
    private List<BasePager> listpages=new ArrayList<>();

    /**
     *page对应的位置
     *@author 聪明一只哈
     *@time 2018/3/28 15:07
     */
    int position;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=(RadioGroup) findViewById(R.id.rg_grops);




        listpages.add(new LocalVideoPager(this));
        listpages.add(new LocalMusicPager(this));
        listpages.add(new NetVideoPager(this));
        listpages.add(new NetMusicPager(this));

        radioGroup.setOnCheckedChangeListener(new MyCheckedChangeListener());

        radioGroup.check(R.id.rb_local_video);


    }



    /**
     *切换的监听
     *@author 聪明一只哈
     *@time 2018/3/28 15:09
     */
    private class MyCheckedChangeListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch(checkedId){
                case R.id.rb_local_music:
                    position=1;

                    break;
                case R.id.rb_net_video:
                    position=2;
                    break;
                case R.id.rb_net_music:
                    position=3;
                    break;

                default:
                    position=0;
                    break;
            }

            setPageFragment();
        }
    }

    /**
     *fragment替换
     *@author 聪明一只哈
     *@time 2018/3/28 15:17
     */
    private void setPageFragment() {

        BasePager basePositionPager = getBasePositionPager();

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        transaction.replace(R.id.fl_pages,new ConMidlesFragment(basePositionPager));

        transaction.commit();


    }

    /**
     *获取对应位置上的page,然后,fragment替换,同时,加载数据
     * basePager.isInitData防止数据重复初始化
     *@author 聪明一只哈
     *@time 2018/3/28 15:17
     */
    private BasePager getBasePositionPager() {
        BasePager basePager = listpages.get(position);
        if(basePager!=null&&!basePager.isInitData){//如果第一次进来没有加载数据,则值为false,做判断,让其请求数据
            basePager.initData();                   //如果请求完数据以后,则值为true,当再次进入时,禁止访问网络,减少请求次数
            basePager.isInitData=true;
        }
        return basePager;
    }
}
